package com.superman.supermarket.client.composite.sku.pop;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent.ToggleSelectEventHandler;
import com.superman.supermarket.client.composite.sku.pop.prop.PropButton;
import com.superman.supermarket.client.composite.sku.pop.prop.PropFlowCell;
import com.superman.supermarket.client.widget.numbereditor.NumCounter;

/**
 * 商品属性选择模块
 * 
 * @author superman
 * @version 2018年8月4日下午9:03:44
 */
public class SkuPropSelectModule extends Composite {

	/**
	 * 回到接口
	 * 
	 * @author superman
	 * @version 2018年8月4日下午11:23:44
	 */
	public interface HideCallBack {
		/**
		 * 关闭按钮
		 * 
		 * @author superman
		 * @version 2018年8月4日 下午11:23:47
		 * @param skuM
		 * @param selectedProp
		 * @param num
		 */
		void close(Map<String, Object> skuM, String selectedProp, int num);

		/**
		 * 确定
		 * 
		 * @author superman
		 * @version 2018年8月4日 下午11:23:49
		 * @param skuM
		 * @param selectedProp
		 * @param num
		 */
		void submit(Map<String, Object> skuM, String selectedProp, int num);
	}

	interface SkuPropSelectModuleUiBinder extends UiBinder<Widget, SkuPropSelectModule> {
	}

	private static SkuPropSelectModuleUiBinder uiBinder = GWT.create(SkuPropSelectModuleUiBinder.class);
	private HideCallBack callback;
	private boolean canBuy = false;
	private Map<String, String> map = new HashMap<String, String>();
	@UiField
	NumCounter numcounter;
	private String prop;
	@UiField
	FlowPanel proppanel;
	private int quota = 0;// 限购,0代表不限购
	@UiField
	Image skuimg;
	private Map<String, Object> skuM;

	private Map<String, Object> skuMap;
	@UiField
	HTML skupnameshtml, salepricehtml, selectedskupnameshtml, stockhtml;

	private JSONArray skuPropList;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月4日 下午9:03:45
	 * @param callback
	 */
	public SkuPropSelectModule(HideCallBack callback) {
		this.callback = callback;
		initWidget(uiBinder.createAndBindUi(this));
	}

	private void changeSkuInfo() {
		skuimg.setUrl(String.valueOf(skuM.get("small_pic")));
		salepricehtml.setText("¥" + String.valueOf(skuM.get("sale_price")));
		selectedskupnameshtml.setText("已选:" + String.valueOf(skuM.get("prop_names")));

		int stock = (int) Double.parseDouble(String.valueOf(skuM.get("stock")));

		if (stock == 0) {
			// 库存不足
			stockhtml.setHTML("<span class='base-color'>已卖完</span>");
			numcounter.setVisible(false);
			canBuy = false;
		} else {
			if (quota == 0) {
				stockhtml.setHTML("剩余库存:" + stock);
				numcounter.setMax(stock);
			} else {
				stockhtml.setHTML("剩余库存:" + stock + "&nbsp;&nbsp;<span class='base-color'>限购:" + quota + "</span>");
				numcounter.setMax(stock > quota ? quota : stock);
			}
			canBuy = true;
		}

	}

	@UiHandler("close")
	void close(ClickEvent event) {
		callback.close(skuM, prop, numcounter.getValue());
	}

	// 获取商品属性的key
	private void getSkuByPropKey(String propKey) {
		String[] ss = propKey.split(":");
		map.put(ss[0], ss[1]);

		String k = "";
		if (map.size() == skuPropList.size()) {
			for (String name : skuMap.keySet()) {
				String[] s = name.split(";");
				for (int i = 0; i < s.length; i++) {
					String[] sss = s[i].split(":");
					String key = sss[0];
					String value = sss[1];
					if (map.get(key) == null || !map.get(key).equals(value)) {
						break;
					}
					if (i == s.length - 1) {
						k = name;
						break;
					}
				}
			}
			prop = k;
			skuM = XJsonUtil.getMap((JSONObject) skuMap.get(k));
			changeSkuInfo();
		}
	}

	/**
	 * 植入数据
	 * 
	 * @author superman
	 * @version 2018年8月4日 下午10:02:54
	 * @param skuDetailMap
	 * @param prop
	 */
	public void setData(Map<String, Object> skuDetailMap, String prop) {
		skuPropList = (JSONArray) skuDetailMap.get("SKU_PROP");
		skuMap = XJsonUtil.getMap((JSONObject) skuDetailMap.get("SKU_MAP"));

		quota = (int) Double.parseDouble(String.valueOf(skuDetailMap.get("QUOTA")));

		String skuPNames = "";

		for (int i = 0; i < skuPropList.size(); i++) {
			PropFlowCell propFlowCell = new PropFlowCell();
			propFlowCell.addToggleSelectEventHandler(new ToggleSelectEventHandler() {
				@Override
				public void onSelected(ToggleSelectEvent event) {
					PropButton pb = (PropButton) event.getToggleItem();
					String propKey = pb.getPropKey();
					getSkuByPropKey(propKey);
				}
			});
			propFlowCell.setData(XJsonUtil.getMap(skuPropList.get(i).isObject()), prop);
			if (i < skuPropList.size())
				propFlowCell.addStyleName("bd-b-1px ddd");
			proppanel.add(propFlowCell);

			skuPNames += "、";
			skuPNames += skuPropList.get(i).isObject().get("SKU_PNAME").isString().stringValue();
		}

		skupnameshtml.setText(skuPNames.substring(1));
	}

	@UiHandler("submitbtn")
	void submit(ClickEvent event) {
		if (canBuy)
			callback.submit(skuM, prop, numcounter.getValue());
		else
			XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("库存不足"));
	}
}
